package com.tledu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tledu.commont.ResultVo;
import com.tledu.pojo.Section;
import com.tledu.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author: 万秋泊
 * @Date: 2022/5/24 14:14
 * @Description: 章节Controller
 */
@RestController
@RequestMapping("/section")
public class SectionController {
    @Autowired
    private SectionService sectionService;

    /**
     * 查询课程对应的章节列表
     *
     * @param course_id 课程id
     * @return
     */
    @GetMapping("/list/{course_id}")
    public List<Section> getSectionList(@PathVariable("course_id") Integer course_id) {
        return sectionService.selectCourseSections(course_id);
    }

    /**
     * 添加章节
     *
     * @param section 章节实体
     * @return
     */
    @PostMapping("/add")
    public ResultVo addSection(@RequestBody Section section) {
        boolean result = sectionService.save(section);
        if (result) {
            return new ResultVo(200, "添加成功", null);
        }
        return new ResultVo(401, "添加失败", null);
    }

    /**
     * 删除子章节
     *
     * @param section_id 章节id
     * @return ResultVo
     */
    @DeleteMapping("/deleteSonSections/{section_id}")
    public ResultVo deleteSonSections(@PathVariable("section_id") Integer section_id) {
        if (sectionService.removeById(section_id)) {
            return new ResultVo(200, "删除成功", null);
        }
        return new ResultVo(401, "删除失败", null);
    }

    /**
     * 删除父章节及其对应的子章节
     *
     * @param section_id 父章节id
     * @return ResultVo
     */
    @DeleteMapping("/deleteParentSections/{section_id}")
    public ResultVo deleteParentSections(@PathVariable("section_id") Integer section_id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("parent_id", section_id);
        sectionService.removeByMap(map);
        if (sectionService.removeById(section_id)) {
            return new ResultVo(200, "删除成功", null);
        }
        return new ResultVo(401, "删除失败", null);
    }

    /**
     * 更新章节
     *
     * @param section 章节对象
     * @return ResultVo
     */
    @PutMapping("/update")
    public ResultVo updateSection(@RequestBody Section section) {
        boolean result = sectionService.updateById(section);
        if (result) {
            return new ResultVo(200, "修改成功", null);
        }
        return new ResultVo(401, "修改失败", null);
    }
}
